package com.rose;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegServlet
 */
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RegServlet() {
        // TODO Auto-generated constructor stub
    }

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException
	{
		String name = arg0.getParameter("name");
		String pass = arg0.getParameter("pass");
		if(null == name || null == pass)
		{
			arg0.setAttribute("tip", "this can't blank");
			forward("/reg.jsp", arg0, arg1);
		}
		
		try
		{
			if(ChatService.instance().addUser(name, pass))
			{
				arg0.setAttribute("tip", "successfu register");
				forward("/reg.jsp", arg0, arg1);
			}
			else
			{
				arg0.setAttribute("tip", "try again");
				forward("/reg.jsp", arg0, arg1);
			}
		} catch (Exception e)
		{
			// TODO: handle exception
		}
		super.service(arg0, arg1);
	}

	private void forward(String url, HttpServletRequest request, HttpServletResponse response)
	{
		ServletContext sc = getServletConfig().getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(url);
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
